<script setup lang="ts">
import { getTokens, getModels } from "@/http/deepseek";
import { API_CONFIG } from "@/config/deepseekConfig";
import {
  useGeneralDataStore,
  useChatDataStore,
  useSettingStore,
} from "@/store";
import { chatNowTime } from "@/utils";
import { useWindowSize } from "@vueuse/core";
import type { ModuleList } from "@/types";
import OpenAI from "openai";

const { width } = useWindowSize();
const settingStore = useSettingStore();
const chatDataStore = useChatDataStore();
const generalDataStore = useGeneralDataStore();
const currentConfig = ref(API_CONFIG);

// 初始化 OpenAI API 客户端
const initOpenAI = () => {
  generalDataStore.openai = new OpenAI({
    ...currentConfig.value,
    // 浏览器环境需要这个选项
    dangerouslyAllowBrowser: true,
  });
  console.log("openai ===>", generalDataStore.openai);
};
// 初始化分类历史对话
const initChatList = () => {
  console.log("分类历史对话 ===>", chatNowTime(chatDataStore.categorizedChats));
};
// 初始化配置
const initConfig = () => {
  console.log("初始化配置 ===>", width.value);
  settingStore.setSmallCollapse(width.value <= 1024);
  settingStore.setPhoneCollapse(width.value <= 768);
};
const initializeData = async () => {
  try {
    const [tokens, models] = await Promise.all([
      // 获取可用得token
      getTokens({ deepseek: "Y", gptToken: API_CONFIG.apiKey }),
      // 获取模型
      getModels({ deepseek: "Y", gptToken: API_CONFIG.apiKey }),
    ]);
    generalDataStore.moduleList = models as unknown as ModuleList;
    console.log("初始化数据完成", { tokens, models });
  } catch (error) {
    console.error("初始化失败:", error);
  }
};
// 挂载
onMounted(async () => {
  initOpenAI();
  initChatList();
  initConfig();
  await initializeData();
  chatDataStore.loadAllSessions();
});
</script>

<template>
  <RouterView />
</template>

<style lang="scss">
@use "./styles/index.scss";
</style>
